var app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderData: {},
    arriveGrossWeight: '', //到厂毛重
    arriveTareWeight: '', //到厂车重
    arriveCargoWeight: '', //到厂货重
    arrivePacketNumber: '', //到厂包数
    weightOne: '', //扣水
    weightTwo: '', //扣杂
    weightNet: '', //净重
    price: '',
    countPrice: 0,
    items: [
      {
        name: '2',
        value: '是',
        checked: false
      },
      {
        name: '1',
        value: '否',
        checked: false
      }
    ],
    settlementType: '', //1结算 、2 不结算
    images_arr: []
  },
  upload: function(item) {
    var that = this
    wx.showLoading({
      title: '上传中'
    })
    wx.uploadFile({
      url: app.preUrl + 'uploadFile',
      header: {
        'Content-Type': 'multipart/form-data'
      },
      filePath: item,
      name: 'file',
      formData: {
        userId: app.globalData.userId //附加信息为用户ID
      },
      success: function(res) {
        console.log(res)
        wx.hideLoading()
        var imgUrl =
          'https://www.lvdaxia.com.cn/dabaozhan/fileImage/' +
          JSON.parse(res.data).result
        that.setData({
          images_arr: that.data.images_arr.concat(imgUrl)
        })
      }
    })
  },
  upimg: function() {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        const tempFilePaths = res.tempFilePaths
        console.log(res)
        tempFilePaths.forEach(function(item, index) {
          that.upload(item)
        })
      }
    })
  },

  // 删除图片
  deleteImg: function(e) {
    var that = this
    var images = that.data.images_arr
    var index = e.currentTarget.dataset.index
    images.splice(index, 1)
    that.setData({
      images_arr: images
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    let data = JSON.parse(options.data)
    console.log(data)
    that.setData({
      orderData: data
    })
  },
  //是否结算
  radioChange: function(e) {
    var that = this
    console.log(e.detail.value)
    that.setData({
      settlementType: e.detail.value
    })
  },
  //录入到厂毛重
  onNumberGrossWeight: function(e) {
    var that = this
    that.setData({ arriveGrossWeight: e.detail.value.replace(/[^\d.]/g, '') })
    that.setData({ arriveGrossWeight: e.detail.value.replace(/\.{2,}/g, '.') })
    that.setData({
      arriveGrossWeight: e.detail.value
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
    })
    that.setData({
      arriveGrossWeight: e.detail.value.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        '$1$2.$3'
      )
    })
    this.getHzWeight()
  },
  //录入到厂车重
  onNumberTareWeight: function(e) {
    var that = this
    that.setData({ arriveTareWeight: e.detail.value.replace(/[^\d.]/g, '') })
    that.setData({ arriveTareWeight: e.detail.value.replace(/\.{2,}/g, '.') })
    that.setData({
      arriveTareWeight: e.detail.value
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
    })
    that.setData({
      arriveTareWeight: e.detail.value.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        '$1$2.$3'
      )
    })
    this.getHzWeight()
  },
  getHzWeight() {
    //获取货重
    let arriveGrossWeight = Number(this.data.arriveGrossWeight) //毛重量
    let arriveTareWeight = Number(this.data.arriveTareWeight) //车货重
    let arriveCargoWeight = arriveGrossWeight - arriveTareWeight
    if (arriveCargoWeight < 0) {
      this.setData({
        arriveCargoWeight: 0,
        weightOneB: 0,
        weightTwoB: 0
      })
    } else {
      this.setData({ arriveCargoWeight: arriveCargoWeight })
      let weightOne = Number(this.data.weightOne)
      if (weightOne > 0) {
        let weightOneB = parseFloat(
          (this.data.weightOne / arriveCargoWeight) * 100
        ).toFixed(2)
        this.setData({ weightOneB: weightOneB })
      }
      let weightTwo = Number(this.data.weightTwo)
      if (weightTwo > 0) {
        let weightTwoB = parseFloat(
          (this.data.weightTwo / arriveCargoWeight) * 100
        ).toFixed(2)
        this.setData({ weightTwoB: weightTwoB })
      }
    }
    this.getJzWeight()
  },
  //录入到厂包数
  onNumberPacketNumber: function(e) {
    var that = this
    that.setData({ arrivePacketNumber: e.detail.value.replace(/[^\d.]/g, '') })
    that.setData({
      arrivePacketNumber: e.detail.value.replace(/\.{2,}/g, '.')
    })
    that.setData({
      arrivePacketNumber: e.detail.value
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
    })
    that.setData({
      arrivePacketNumber: e.detail.value.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        '$1$2.$3'
      )
    })
  },

  //录入扣水重量
  onNumberWeightOne: function(e) {
    var that = this
    that.setData({ weightOne: e.detail.value.replace(/[^\d.]/g, '') })
    that.setData({ weightOne: e.detail.value.replace(/\.{2,}/g, '.') })
    that.setData({
      weightOne: e.detail.value
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
    })
    that.setData({
      weightOne: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    })
    let arriveCargoWeight = Number(this.data.arriveCargoWeight) //货重
    if (arriveCargoWeight > 0) {
      let weightOneB = parseFloat(
        (e.detail.value / arriveCargoWeight) * 100
      ).toFixed(2)
      that.setData({ weightOneB: weightOneB })
    } else {
      that.setData({ weightOneB: 0 })
    }
    that.getJzWeight()
  },
  onNumberWeightOneB(e) {
    var that = this
    that.setData({
      weightOneB: e.detail.value.replace(/[^\d.]/g, '')
    })
    that.setData({
      weightOneB: e.detail.value.replace(/\.{2,}/g, '.')
    })
    that.setData({
      weightOneB: e.detail.value
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
    })
    that.setData({
      weightOneB: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    })
    let arriveCargoWeight = Number(this.data.arriveCargoWeight) //货重
    if (arriveCargoWeight > 0) {
      let weightOne = parseFloat(
        (e.detail.value * arriveCargoWeight) / 100
      ).toFixed(2)
      that.setData({
        weightOne: weightOne
      })
    } else {
      that.setData({
        weightOne: 0
      })
    }
    this.getJzWeight()
  },
  //录入扣杂重量
  onNumberWeightTwo: function(e) {
    var that = this
    that.setData({ weightTwo: e.detail.value.replace(/[^\d.]/g, '') })
    that.setData({ weightTwo: e.detail.value.replace(/\.{2,}/g, '.') })
    that.setData({
      weightTwo: e.detail.value
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
    })
    that.setData({
      weightTwo: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    })
    let arriveCargoWeight = Number(this.data.arriveCargoWeight) //货重
    if (arriveCargoWeight > 0) {
      let weightTwoB = parseFloat(
        (e.detail.value / arriveCargoWeight) * 100
      ).toFixed(2)
      that.setData({ weightTwoB: weightTwoB })
    } else {
      that.setData({ weightTwoB: 0 })
    }
    that.getJzWeight()
  },
  onNumberWeightTwoB(e) {
    var that = this
    that.setData({
      weightTwoB: e.detail.value.replace(/[^\d.]/g, '')
    })
    that.setData({
      weightTwoB: e.detail.value.replace(/\.{2,}/g, '.')
    })
    that.setData({
      weightTwoB: e.detail.value
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
    })
    that.setData({
      weightTwoB: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    })
    let arriveCargoWeight = Number(this.data.arriveCargoWeight) //货重
    if (arriveCargoWeight > 0) {
      let weightTwo = parseFloat(
        (e.detail.value * arriveCargoWeight) / 100
      ).toFixed(2)
      that.setData({
        weightTwo: weightTwo
      })
    } else {
      that.setData({
        weightTwo: 0
      })
    }
    this.getJzWeight()
  },
  //录入单价
  onNumberPrice: function(e) {
    var that = this
    that.setData({ price: e.detail.value.replace(/[^\d.]/g, '') })
    that.setData({ price: e.detail.value.replace(/\.{2,}/g, '.') })
    that.setData({
      price: e.detail.value
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
    })
    that.setData({
      price: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    })
    this.getCountPrice()
  },
  getJzWeight() {
    //获取净重
    let weightOne = Number(this.data.weightOne) //扣水重量
    let weightTwo = Number(this.data.weightTwo) //扣杂重量
    let arriveCargoWeight = Number(this.data.arriveCargoWeight) //货重
    let weightNet = arriveCargoWeight - weightOne - weightTwo
    if (weightNet < 0) {
      this.setData({ weightNet: 0 })
    } else {
      this.setData({ weightNet: weightNet })
    }
    this.getCountPrice()
  },
  getCountPrice() {
    //获取总价
    let price = Number(this.data.price) //单价
    let weightNet = Number(this.data.weightNet) //净重
    let countPrice = price * weightNet
    this.setData({ countPrice: countPrice })
  },
  bindFormSubmit: function() {
    var that = this
    var orderSerial = that.data.orderData.orderSerial //出库流水号
    var arriveGrossWeight = that.data.arriveGrossWeight //到厂毛重
    var arriveTareWeight = that.data.arriveTareWeight //到厂车重
    var arriveCargoWeight = that.data.arriveCargoWeight //到厂货重
    var arrivePacketNumber = that.data.arrivePacketNumber //到厂包数
    var weightOne = that.data.weightOne //扣水
    var weigtTwo = that.data.weightTwo //扣杂
    var weightNet = that.data.weightNet //净重
    var price = that.data.price //单价
    var countPrice = that.data.countPrice //总价
    var settlementType = that.data.settlementType
    if (
      arriveGrossWeight === '' ||
      arriveTareWeight === '' ||
      arriveCargoWeight === '' ||
      arrivePacketNumber === '' ||
      weightOne === '' ||
      weigtTwo === '' ||
      weightNet === '' ||
      price === '' ||
      countPrice == '' ||
      settlementType == ''
    ) {
      wx.showToast({ title: '参数缺失,请检查', icon: 'none' })
      return false
    } else {
      wx.showLoading({
        title: '提交中'
      })
      that.setData({ buthidden: true })
      wx.request({
        method: 'POST',
        url: app.preUrl + 'updateArriveSite',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: app.globalData.userId, //
          destination: that.data.orderData.destination, //厂名
          orderSerial: orderSerial, //出库流水号
          arriveGrossWeight: arriveGrossWeight, //到厂毛重
          arriveTareWeight: arriveTareWeight, //到厂车重
          arriveCargoWeight: arriveCargoWeight, //到厂货重
          arrivePacketNumber: arrivePacketNumber, //到厂包数
          arriveNetWeight: weightNet, //到厂净重
          arriveOffWater: weightOne, //到厂扣水重量
          arriveOffOther: weigtTwo, //到厂扣杂重量
          arrivePrice: price, //到厂单价
          arriveMoney: countPrice, //到厂总金额
          arrivePic: that.data.images_arr, //到厂图片
          settleStatus: settlementType, //1未结款 2已结款
          categoryId: that.data.orderData.categoryId,
          parentId: that.data.orderData.parentId
        },
        success: function(res) {
          console.log(res)
          wx.hideLoading()
          if (res.data.code == 100) {
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              duration: 1000
            })
            setTimeout(function() {
              wx.redirectTo({ url: 'sales_statement' })
            }, 1500)
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false
            })
            that.setData({ buthidden: false })
          }
        },
        fail: function(error) {
          console.log(error)
          that.setData({ buthidden: false })
        }
      })
    }
  }
})
